"""
数据导入管理命令
"""
import json
import os
from django.core.management.base import BaseCommand
from apps.attractions.models import Attraction


class Command(BaseCommand):
    help = '导入南昌景点数据'

    def handle(self, *args, **options):
        # 获取JSON文件路径
        json_path = os.path.join(
            os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
            '..',
            'database',
            'mock_data',
            'nanchang_attractions.json'
        )
        
        if not os.path.exists(json_path):
            self.stdout.write(self.style.ERROR(f'文件不存在: {json_path}'))
            return
        
        # 读取JSON数据
        with open(json_path, 'r', encoding='utf-8') as f:
            attractions_data = json.load(f)
        
        # 导入数据
        count = 0
        for data in attractions_data:
            attraction, created = Attraction.objects.get_or_create(
                name=data['name'],
                defaults={
                    'latitude': data['latitude'],
                    'longitude': data['longitude'],
                    'ticket_price': data['ticket_price'],
                    'visit_time': data['visit_time'],
                    'popularity': data['popularity'],
                    'description': data.get('description', ''),
                    'address': data.get('address', '')
                }
            )
            if created:
                count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'✓ 导入景点: {attraction.name}')
                )
            else:
                self.stdout.write(
                    self.style.WARNING(f'- 景点已存在: {attraction.name}')
                )
        
        self.stdout.write(
            self.style.SUCCESS(f'\n成功导入 {count} 个景点')
        )

